<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/14
 * Time: 14:10
 */

namespace app\controllers;

use app\librarys\ExpressQuery;
use app\librarys\PayProController;
use app\librarys\Tools;
use app\models\CategoryExt;
use app\models\db\Category;
use app\models\db\Express;
use app\models\db\Goods;
use app\models\db\GoodsImage;
use app\models\db\MiniPrograms;
use app\models\db\TUser;
use app\models\File;
use app\models\GoodsExt;
use app\models\GoodsQuery;
use app\models\Manager;
use app\models\MiniProgramsExt;
use app\models\User;
use yii\data\Pagination;
use yii\web\NotFoundHttpException;


class GoodsController extends PayProController
{

    public $enableCsrfValidation = false;


    /**
     * 获取分类
     * @return string
     */
    public function actionCategory()
    {
        $mini_program = '1';
        $items = CategoryExt::getCategoryTreeTable($mini_program);
        return $this->render('category', ['items' => $items]);
    }


    public function actionCategoryGoodsSaveHandler(){
//        var_dump('in action');
        $request = \Yii::$app->request;
        $checkbox_select=$_POST["check"];

        $category_id = $request->post('category_id');
        var_dump($category_id);
        $category_id = $_POST['category_id'];
        var_dump($category_id);
        for($i=0;$i<count($checkbox_select);$i++)
        {
//            echo "选项".$checkbox_select[$i]."被选中<br />";
//            var_dump($checkbox_select[$i]);
        }

        CategoryExt::setCategoryGoods($checkbox_select,$category_id);


        return $this->renderJson(null, 0, '操作成功');
    }

    public function actionCategoryGoodsPartial(){
        $request = \Yii::$app->request;
//        $category_id = $request->post('category_id');

        $category_id = $request->get('category_id');
//        $session = \Yii::$app->session;
        $items = [];

//        $manager = Manager::getCurrentManager();
//        $manager_id = $manager['id'];
//        if(empty($category_id)){
//            $category_id = \Yii::$app->session->get('category_id_');
//        }else{
//            \Yii::$app->session->set('category_id_', $category_id);
//        }


//        var_dump($category_id);
        $category = Category::findOne($category_id);
//        var_dump($category->id);
//        exit();
        $mini_program = MiniPrograms::findOne($category->mini_programs_id);
        $shops = $mini_program->getTEShops();
        $count = GoodsExt::getGoodsCount('',$mini_program->id,'');
        $pagination = new Pagination([
            'totalCount' => $count
        ]);
        $items = GoodsExt::getGoodsList($pagination->page, $pagination->pageSize, '',$mini_program->id);

        return $this->renderPartial('category-goods-partial', [
            'goods_items' => $items,
            'category_id' => $category_id,
            'pagination' => $pagination
        ]);

    }


    public function actionCategoryPartial()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $operate = $request->post('operate');
        $item = [];
        if ($operate == 'add') {
            $data = CategoryExt::getCategoryById($id);
            if (isset($data)) {
                $item['parent_id'] = $data['id'];
                $item['parent_name'] = $data['name'];
            } else {
                $item['parent_id'] = '';
                $item['parent_name'] = '商品分类';
            }
        } elseif ($operate == 'edit') {
            $item = CategoryExt::getCategoryById($id)->attributes;
            $parent_item = CategoryExt::getCategoryById($item['parent_id']);
            if (isset($parent_item)) {
                $item['parent_name'] = $parent_item['name'];
            } else {
                $item['parent_name'] = '商品分类';
            }
        }
        return $this->renderPartial('category-partial', [
            'item' => $item
        ]);
    }

    public function actionCategorySaveHandler()
    {
        $mini_program = '1';

        $request = \Yii::$app->request;
        $id = $request->post('id');
        $name = $request->post('name');
        $code = $request->post('code');
        $parent_id = $request->post('parent_id');
        $is_enabled = $request->post('is_enabled');
        $is_show = $request->post('is_show');
        $sort = $request->post('sort');

        $img_path = $request->post('img_path');

        $image_id = '';
        if ($img_path) {
            $image_id = File::UploadCutImage($img_path);
        }

//        $current_user = Manager::getCurrentManager();



        $result = CategoryExt::saveCategory($mini_program, $id, $code, $name, $parent_id, $image_id, $is_show, $sort, $is_enabled);

        if ($result) {
            return $this->renderJson(null, 0, '操作成功');
        }

        return $this->renderJson(null, 1, '操作失败');
    }

    public function actionCategoryDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $result = CategoryExt::deleteCategory($id);
        if ($result) {
            return $this->renderJson(null, 0, '操作成功');
        }

        return $this->renderJson(null, 1, '操作失败');
    }

    /**
     *
     * @return string
     */
    public function actionGoods()
    {
        $mini_programs_id = '1';
        $request = \Yii::$app->request;

        //获取mini_program id，判断是否在当前登录用户的小程序权限内

        $mini_programs_id = $request->get('mini_program_id');
        if(!Manager::isHasMiniProgram($mini_programs_id)){
            var_dump('你没有该小程序的权限');exit;
        }

        $name = $request->get('name');

        $count = GoodsExt::getGoodsCount($name,$mini_programs_id);
        $pagination = new Pagination([
            'totalCount' => $count
        ]);
        $items = GoodsExt::getGoodsList($pagination->page, $pagination->pageSize, $name,$mini_programs_id);

        return $this->render('goods',
            [
                'items' => $items,
                'name' => $name,
                'pagination' => $pagination
            ]);
    }

    public function actionGoodsPartial()
    {
        $this->layout = false;
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $item = GoodsExt::getGoodsById($id);
        $pro_images = GoodsExt::getGoodsImages($id, 2);
        $detail_images = GoodsExt::getGoodsImages($id, 3);
        return $this->render('goods-partial',
            [
                'item' => $item,
                'pro_images' => $pro_images,
                'detail_images' => $detail_images
            ]
        );
    }

    public function actionGoodsDeleteImageHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $img_id = $request->post('img_id');

        $result = GoodsExt::deleteGoodsImage($id,$img_id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionGoodsSaveHandler()
    {
        $mini_program = '1';
        $request = \Yii::$app->request;
        $id = $request->post('id');
        $name = $request->post('name');
        $summary = $request->post('summary');
        $unit = $request->post('unit');
        $stock = $request->post('stock');
        $settlement_price = $request->post('settlement_price');
        $selling_price = $request->post('selling_price');
        $old_price = $request->post('old_price');
        $is_enabled = $request->post('is_enabled');
        $is_shelved = $request->post('is_shelved');
        $is_recommend = $request->post('is_recommend');
        $is_newest = $request->post('is_newest');
        $is_hot = $request->post('is_hot');
        $is_free_express = $request->post('is_free_express');
        $is_promotion = $request->post('is_promotion');
        $sell_count = $request->post('sell_count');
        $collected_count = $request->post('collected_count');
        $express_money = $request->post('express_money');
        $is_invoice = $request->post('is_invoice');
        $is_GGCT = $request->post('is_GGCT');
        $is_seven_day = $request->post('is_seven_day');
        $is_guarantee = $request->post('is_guarantee');
        $is_return_goods = $request->post('is_return_goods');
        $sort = $request->post('sort');

        $small_img_path = $request->post('small_img_path');

        $image_id = '';
        if ($small_img_path) {
            $image_id = File::UploadCutImage($small_img_path);
        }


        $pro_img_paths = $request->post('pro_img_paths');
        $pro_img_ids = [];
        if ($pro_img_paths && count($pro_img_paths) > 0) {
            foreach ($pro_img_paths as $pro_img_path) {
                array_push($pro_img_ids, File::UploadCutImage($pro_img_path));
            }
        }

        $detail_img_paths = $request->post('detail_img_paths');
        $detail_img_ids = [];
        if ($detail_img_paths && count($detail_img_paths) > 0) {
            foreach ($detail_img_paths as $detail_img_path) {
                array_push($detail_img_ids, File::UploadCutImage($detail_img_path));
            }
        }


        $result = GoodsExt::saveGoods($id, $name, '', '', '', $summary, $unit, $stock, $settlement_price,
            $selling_price, $old_price, $is_enabled, $is_shelved, '', $is_recommend, $is_newest, $is_hot, $is_free_express,
            $is_promotion, '', '', '', '', '', $sell_count, $collected_count, $express_money,
            $is_invoice, $is_GGCT, $is_seven_day, $is_guarantee, $is_return_goods, $mini_program, '',
            $sort, $image_id, $pro_img_ids, $detail_img_ids);

//        $result = GoodsExt::saveGoods($id, $name, $raw_id, $app_id, $app_secret, $type, $begin_datetime, $end_datetime, $is_enabled);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

    public function actionGoodsDeleteHandler()
    {
        $request = \Yii::$app->request;
        $id = $request->post('id');

        $result = GoodsExt::deleteGoods($id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '操作成功' : '操作失败');
    }

}